<?php

namespace app\user\model;

use think\Model;

class CommentModel extends Model
{
    /**
     * 关联 user表
     * @return $this
     */
    public function user()
    {
        return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
    }


    /**
     * content 自动转化
     * @param $value
     * @return string
     */
    public function getContentAttr($value)
    {
        return cmf_replace_content_file_url(htmlspecialchars_decode($value));
    }

    /**
     * content 自动转化
     * @param $value
     * @return string
     */
    public function setContentAttr($value)
    {

        $config = \HTMLPurifier_Config::createDefault();
        if (!file_exists(RUNTIME_PATH . 'HTMLPurifier_DefinitionCache_Serializer')) {
            mkdir(RUNTIME_PATH . 'HTMLPurifier_DefinitionCache_Serializer');
        }

        $config->set('Cache.SerializerPath', RUNTIME_PATH . 'HTMLPurifier_DefinitionCache_Serializer');
        $purifier  = new \HTMLPurifier($config);
        $cleanHtml = $purifier->purify(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
        return htmlspecialchars($cleanHtml);
    }

    //阿里云上传图片

    //阿里云上传

    public function aliyun($info,$isunlink=false,$bucket="hrtvoss"){
        $accessKeyId = "0mFOYyKd94aev384";//去阿里云后台获取秘钥
        $accessKeySecret = "YGiORi102VjNHRmxWkrlMzuUaAqA3a";//去阿里云后台获取秘钥
        $endpoint = "oss-cn-beijing.aliyuncs.com";//你的阿里云OSS地址


        import('Aliyun/autoload','../simplewind/extend/','.php');//引入文件
//        import('Aliyun/autoload','/application/article/simplewind/extend/','.php');//引入文件
        $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);

        //        判断bucketname是否存在，不存在就去创建
        if( !$ossClient->doesBucketExist($bucket)){
            $ossClient->createBucket($bucket);
        }
        $way=empty($way)?$bucket:$way;

        $object = 'content/'.date('Ymd').'/'.time().'/'.$info['name'];//想要保存文件的名称
        $file = ROOT_PATH.'/public/'.$info['url'];//文件路径，必须是本地的。

        try{
            $res    =   $ossClient->uploadFile($bucket,$object,$file);
            if ($isunlink==true){
                unlink($file);
            }
        }catch (OssException $e){
            $e->getErrorMessage();
        }
        $oss="";

        if($res['info']['http_code'] == '200'){
            $resd['url']	=	$res['info']['url'];
            $resd['file']	=	$res['info']['url'];
            $resd['error']	=	0;
            unlink($file);
            return  $resd;
        } else {
            $resd['url']	=	$res['info']['url'];
            $resd['file']	=	$res['info']['url'];
            $resd['error']	=	0;
            return  $resd;
        }
    }


}

